from PyQt5 import uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import QStringListModel
import shutil
import os
from PyQt5.QtWidgets import QInputDialog, QLineEdit
from PyQt5.QtWidgets import QProgressBar

from PyQt5 import QtCore
from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo


import debugPrinter as dp


from PyQt5.QtWidgets import QLabel,QDialog,QVBoxLayout,QHBoxLayout

import os
import json
import constantValues as cv


qt_creator_file = os.path.join(os.path.dirname(__file__),"ui_config", "setting.ui")

win, QtBaseClass = uic.loadUiType(qt_creator_file)


class SETTING(QtWidgets.QWidget, win):

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        win.__init__(self)

        self.setupUi(self)

        # self.port = QSerialPort()

        # # self.setup_serial()
        # self.find_serial_ports()

        self.btn_open_com.clicked.connect(self.open_com_btn_click)
        self.btn_set_wifi.clicked.connect(self.set_wifi_btn_click)
        self.btn_set_pw.clicked.connect(self.set_pw_btn_click)
        self.btn_set_hostIP.clicked.connect(self.set_hostIP_btn_click)
        self.btn_set_port.clicked.connect(self.set_port_btn_click)

        # self.timer = QtCore.QTimer()
        # # self.timer.setInterval(50)
        # self.timer.setInterval(100)
        # # self.timer.setSingleShot(True)
        # self.timer.timeout.connect(self.timer_handler)

    def __del__(self):
        dp.dpt('del setting' )
        # try:
        #     if self.port.isOpen():
        #         self.port.close()
        #         self.timer.stop()
        #         print("serial closed on exit")
        # except:
        #     pass  # XXX errors on shutdown
            
    def timer_handler(self):
        data = self.port.readLine()
        if len(data)>0:
            print(data)

    # def setup_serial(self):
    def set_wifi_btn_click(self):
        json_tmp={cv.JSON_ssid:self.lineEdit_ssid.text()}
        json_tmp_str = json.dumps(json_tmp)
        self.port.write(bytes(json_tmp_str,"ascii"))
    def set_pw_btn_click(self):
        json_tmp={cv.JSON_pw:self.lineEdit_pw.text()}
        json_tmp_str = json.dumps(json_tmp)
        self.port.write(bytes(json_tmp_str,"ascii"))
    def set_hostIP_btn_click(self):
        print(self.lineEdit_hostIP.text())
        json_tmp={cv.JSON_hostTP:self.lineEdit_hostIP.text()}
        json_tmp_str = json.dumps(json_tmp)
        self.port.write(bytes(json_tmp_str,"ascii"))
    def set_port_btn_click(self):
        json_tmp={cv.JSON_port:self.lineEdit_port.text()}
        json_tmp_str = json.dumps(json_tmp)
        self.port.write(bytes(json_tmp_str,"ascii"))

    def open_com_btn_click(self):
        if self.comboBox_serialPort.count() > 0:
            self.port.setPortName(self.comboBox_serialPort.currentText())
        else:
            return 0

        self.port.setBaudRate(115200)
        self.port.setDataBits(QSerialPort.Data8)
        self.port.setStopBits(QSerialPort.OneStop)
        self.port.setParity(QSerialPort.NoParity)

        r = self.port.open(QtCore.QIODevice.ReadWrite);
        if r:
            print("open suc")
            self.timer.start()
        else:
            print("open failed")
        
        return r

    def find_serial_ports(self):
        port_list = QSerialPortInfo.availablePorts()           
        if len(port_list)==0:
            print("no serial port was found")
            return

        for l in port_list:
            self.comboBox_serialPort.addItem(l.portName())

